<?php

namespace App\Http\Controllers\Api;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Model\User;
use App\Common\Service\ArticleService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;



class ArticleController extends Controller
{
    public $articleService;

    /**
     * UserController constructor.
     * @param $articleService
     */
    public function __construct(ArticleService $articleService)
    {
        $this->articleService = $articleService;
    }
    /**
     * 文章列表
     * @param User $user
     * @return string
     */
    public function articleList(Request $request)
    {
        $cateId = $request->get('cate_id');
        if (empty($cateId)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }


        $articles = $this->articleService->getArticlesByCateId($cateId);
        Log::info("获取文章分类信息：".json_encode($articles));
        if (empty($articles)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $page_crumbs = $this->articleService->getCrumbsbyId($cateId);
        $articles['page_crumbs'] = $page_crumbs;

        $data = $articles;

        return ApiResponse($articles);

    }

    /**
     * 文章详情
     * @param Request $request
     * @return mixed
     */
    public function detail(Request $request)
    {
        $id = $request->get('id');
        if (empty($id) ){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $article = $this->articleService->getArticleDetail($id);
        if (empty($article) ){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        $page_crumbs = $this->articleService->getCrumbsbyId($article['cate_id']);

        $article['page_crumbs'] = $page_crumbs;
        return ApiResponse($article,'success',1);

    }
    /**
     * 所有分类
     * @param Request $request
     * @return mixed
     */
    public function categories(Request $request)
    {
        $p_cate_name = $request->get('p_cate_name');
        if (!$p_cate_name){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $categories =  $this->articleService->getCategories($p_cate_name);
        Log::info("获取文章分类名称：".json_encode($categories));
        if (!$categories){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $data = $categories;

        return ApiResponse($data);

    }

    /**
     * 首页新闻列表
     */
    public function homeList(Request $request){

        $cateIdStr = $request->get('cate_id');
        if(strpos($cateIdStr, ',') === false) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $cateIdArr = explode(',', $cateIdStr);
        foreach ($cateIdArr as $cateId) {
            $articles = $this->articleService->getHomeList($cateId);
            if (empty($articles)) {
                throw new AixinException(AixinExceptionCode::RESULT_NULL);
            }
            $data['recommend_list'][] = $articles;
        }


        return ApiResponse($data);
    }

    /**
     * 相关新闻接口
     */
    public function aboutNews(Request $request){

        $cateId = $request->get('cate_id');
        if (empty($cateId) )  {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $articles = $this->articleService->getAboutNews($cateId);
        if (!$articles){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $data = $articles;
        return ApiResponse($data);

    }

    function search(Request $request)
    {
        $content = $request->get('content');
        $cateId  = $request->get('cate_id');
        $data['list'] = $this->articleService->search($content, $cateId);

        if(count($data) == 0){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        return ApiResponse($data);
    }



}
